<?php
	
	function login($clientID)
	{
		global $connection;
		
		$client = $connection->fetch_row("user", $clientID);
		if(isset($client["id"]))
		{
			$loginKey = md5($client["email"].time());
			
			$_SESSION["client"]["id"] = $client["id"];
			$_SESSION["client"]["login_key"] = $loginKey;
			
			$params["last_login"] = date("Y-m-d H:i:s");
			$params["session_user"] = $loginKey;
			$params["id"] = $client["id"];
			
			$connection->update("user", $params);
		}
	}
	
	function logout()
	{
		global $connection;
		
		if(isset($_SESSION["client"]))
		{
			unset($_SESSION["client"]);
			if(isset($_SESSION["cart"]))
			{
				unset($_SESSION["cart"]);
			}
			
			if(isset($_SESSION["coupons"]))
			{
				unset($_SESSION["coupons"]);
			}
		}
	}
	
	function getLoginData()
	{
		global $connection;
		if(isset($_SESSION["client"]["id"]) && isset($_SESSION["client"]["login_key"]))
		{
			$client = $connection->fetch_one("SELECT * FROM user WHERE id = ? AND session_user = ?", array($_SESSION["client"]["id"], $_SESSION["client"]["login_key"]));
			
			if($client)
			{
				$client["wishlist"] = getWishlist($client["id"]);
				
				return $client;
			}
			else
			{
				unset($_SESSION["client"]);
			}
		}
		return false;
	}
	
	function getWishlist($clientID)
	{
		global $connection;
		
		$wishlist = array();
		
		$wishItems = $connection->fetch_result("SELECT * FROM users_wishlists WHERE user_id = ?", array($clientID));
		foreach($wishItems as $key => $wishItem)
		{
			$wishlist[$wishItem["product_id"]] = getProductByField($wishItem["product_id"], "id");
			$wishlist[$wishItem["product_id"]]["wish_price"] = $wishItem["wish_price"];
			$wishlist[$wishItem["product_id"]]["wishmeter"] = $wishItem["wishmeter"];
		}
		return $wishlist;
	}
	
	function generateCountryList($countryID = 0)
	{
		global $connection;
		
		$countries = $connection->fetch_table("countries", null, "name");
		
		echo "<option value='0'>". translate("select_country") . "</option>";
		foreach ($countries as $country) 
		{
			echo "<option value='{$country["id"]}'";
			if($country["id"] == $countryID)
			{
				echo " selected='selected'";
			}
			echo ">{$country["name"]}</option>";
		}
	}
	
	function generateCountyList($countyID = 0)
	{
		global $connection;
		
		$counties = $connection->fetch_result("SELECT * FROM counties", array());
		
		echo "<option value='0'>". translate("select_county") . "</option>";
		foreach ($counties as $county) 
		{
			echo "<option value='{$county["id"]}'";
			if($county["id"] == $countyID)
			{
				echo " selected='selected'";
			}
			echo ">{$county["name"]}</option>";
		}
	}
	
	function generateCityList($countyID, $cityID = 0)
	{
		global $connection;
		
		$cities = $connection->fetch_result("SELECT * FROM cities WHERE county_id = ?", array($countyID));
		
		echo "<option value='0'>". translate("select_city") . "</option>";
		foreach ($cities as $city) 
		{
			echo "<option value='{$city["id"]}'";
			if($city["id"] == $cityID)
			{
				echo " selected='selected'";
			}
			echo ">{$city["name"]}</option>";
		}
	}
	
	function validateClient(&$params, &$errors, $client = null, $checkPassword = true)
	{
		global $connection;
		
		if(!isset($client["id"]))
		{
			if(getRequestParameter("email") != false)
			{
				$params["email"] = getRequestParameter("email");
				
				if(validateEmail($params["email"]) != true)
				{
					$errors["email"] = translate("register_error_invalid_email");
				}
				
				if($checkPassword == true)
				{
					$checkClient = $connection->fetch_row("user", $params["email"], "email");
					if(isset($checkClient["id"]))
					{
						$errors["email"] = translate("register_error_existing_user");
					}
				}
				
			}
			else
			{
				$errors["email"] = translate("register_error_email");
			}
			
			if($checkPassword == true)
			{
				if(getRequestParameter("terms_and_conditions") === false || getRequestParameter("terms_and_conditions") != 1)
				{
					$errors["terms_and_conditions"] = translate("register_error_terms_and_conditions");
				}
				else
				{
					$params["terms_and_conditions"] = 1;
				}
				
				if(getRequestParameter("password") != false)
				{
					$params["password"] = getRequestParameter("password");
					if(strlen($params["password"]) < 5)
					{
						$errors["password"] = translate("register_error_invalid_password");
					}
					else
					{
						$passwordConfirm = getRequestParameter("confirm_password");
						if($params["password"] !== $passwordConfirm)
						{
							$errors["confirm_password"] = translate("register_error_confirm_password");
						}
						else
						{
							$params["password"] = md5($params["password"]);
						}
					}
				}
				else
				{
					$errors["password"] = translate("register_error_password");
				}
			}
		}
		
		if(getRequestParameter("name") != false)
		{
			$params["name"] = getRequestParameter("name");
		}
		else
		{
			$errors["name"] = translate("register_error_name");
		}
		
		if(getRequestParameter("address") != false)
		{
			$params["address"] = getRequestParameter("address");
		}
		else
		{
			$errors["address"] = translate("register_error_address");
		}
		
		if(getRequestParameter("city_id") != false && getRequestParameter("city_id") != 0)
		{
			$params["city_id"] = getRequestParameter("city_id");
			$city = $connection->fetch_row("cities", $params["city_id"], "id");
			if(isset($city["id"]))
			{
				$params["city_name"] = $city["name"];
			}
// TODO : check if it's an existing one and insert if not
		}
		else
		{
			$errors["city_id"] = translate("register_error_city");
		}
		
		if(getRequestParameter("county_id") != false && getRequestParameter("county_id") != 0)
		{
			$params["county_id"] = getRequestParameter("county_id");
			$county = $connection->fetch_row("counties", $params["county_id"], "id");
			if(isset($county["id"]))
			{
				$params["county_name"] = $county["name"];
			}
// TODO : check if it's an existing one and insert if not
		}
		else
		{
			$errors["county_id"] = translate("register_error_county");
		}
		
/*		if(getRequestParameter("country_id") != false && getRequestParameter("country_id") != 0)
		{
			$params["country_id"] = getRequestParameter("country_id");
			$country = $connection->fetch_row("countries", $params["country_id"], "id");
			if(isset($country["id"]))
			{
				$params["country_name"] = $country["name"];
			}
// TODO :  insert if not
		}
		else
		{
			$errors["country_id"] = translate("register_error_country");
		}
*/	
		if(getRequestParameter("zip_code") != false)
		{
			$params["zip_code"] = getRequestParameter("zip_code");
		}
		else
		{
			$errors["zip_code"] = translate("register_error_zip_code");
		}
		
		if(getRequestParameter("phone") != false)
		{
			$params["phone"] = getRequestParameter("phone");
// TODO : validate phone
		}
		else
		{
			$errors["phone"] = translate("register_error_phone");
		}
		
		if(getRequestParameter("type") !== false)
		{
			$params["type"] = getRequestParameter("type");
			if($params["type"] == 1)
			{
				if(getRequestParameter("fax") != false)
				{
					$params["fax"] = getRequestParameter("fax");
				}
				
				if(getRequestParameter("company_contact_name") != false)
				{
					$params["company_contact_name"] = getRequestParameter("company_contact_name");
				}
				
				if(getRequestParameter("registration_number") != false)
				{
					$params["registration_number"] = getRequestParameter("registration_number");
				}
				
				if(getRequestParameter("unique_code") != false)
				{
					$params["unique_code"] = getRequestParameter("unique_code");
				}
				
				if(getRequestParameter("bank_name") != false)
				{
					$params["bank_name"] = getRequestParameter("bank_name");
				}
				
				if(getRequestParameter("bank_account") != false)
				{
					$params["bank_account"] = getRequestParameter("bank_account");
				}
				
				if(getRequestParameter("company_phone") != false)
				{
					$params["company_phone"] = getRequestParameter("company_phone");
				}
			}
		}
		else
		{
			$errors["type"] = translate("register_error_person_type");
		}
	}

	function validateDeliveryDetails(&$params, &$errors)
	{
		global $connection;
		
		if(getRequestParameter("use_delivery_address") !== false)
		{
			$params["use_delivery_address"] = getRequestParameter("use_delivery_address");
			if($params["use_delivery_address"] == 0)
			{
				$params["delivery_type_text"] = translate("send_to_contact_address");
				$params["delivery_name"] = $params["name"];
				$params["delivery_address"] = $params["address"];
				$params["delivery_country_id"] = $params["country_id"];
				$params["delivery_county_id"] = $params["county_id"];
				$params["delivery_city_id"] = $params["city_id"];
				
				$params["delivery_country_name"] = $params["country_name"];
				$params["delivery_county_name"] = $params["county_name"];
				$params["delivery_city_name"] = $params["city_name"];
				
				$params["delivery_zip_code"] = $params["zip_code"];
				$params["delivery_phone"] = $params["phone"];
			}
			else
			{
				$params["delivery_type_text"] = translate("send_to_other_address");
				
				$params["delivery_name"] = $params["name"];
				$params["delivery_phone"] = $params["phone"];
				
				/*if(getRequestParameter("delivery_name") !== false && getRequestParameter("delivery_name") != "")
				{
					$params["delivery_name"] = getRequestParameter("delivery_name");
				}
				else
				{
					$errors["delivery_name"] = translate("checkout_error_delivery_name");
				}*/
				
				if(getRequestParameter("delivery_address") !== false && getRequestParameter("delivery_address") != "")
				{
					$params["delivery_address"] = getRequestParameter("delivery_address");
				}
				else
				{
					$errors["delivery_address"] = translate("checkout_error_delivery_address");
				}
				
				if(getRequestParameter("delivery_country_id") !== false && getRequestParameter("delivery_country_id") > 0)
				{
					$params["delivery_country_id"] = getRequestParameter("delivery_country_id");
					$country = $connection->fetch_row("countries", $params["delivery_country_id"], "id");
					if(isset($country["id"]))
					{
						$params["delivery_country_name"] = $country["name"];
					}
				}
				else
				{
					$errors["delivery_country_id"] = translate("checkout_error_delivery_country");
				}
				
				if(getRequestParameter("delivery_county_id") !== false && getRequestParameter("delivery_county_id") > 0)
				{
					$params["delivery_county_id"] = getRequestParameter("delivery_county_id");
					$county = $connection->fetch_row("counties", $params["delivery_county_id"], "id");
					if(isset($county["id"]))
					{
						$params["delivery_county_name"] = $county["name"];
					}
				}
				else
				{
					$errors["delivery_county_id"] = translate("checkout_error_delivery_county");
				}
				
				if(getRequestParameter("delivery_city_id") !== false && getRequestParameter("delivery_city_id") > 0)
				{
					$params["delivery_city_id"] = getRequestParameter("delivery_city_id");
					$city = $connection->fetch_row("cities", $params["delivery_city_id"], "id");
					if(isset($city["id"]))
					{
						$params["delivery_city_name"] = $city["name"];
					}
				}
				else
				{
					$errors["delivery_city_id"] = translate("checkout_error_delivery_city");
				}
				
				if(getRequestParameter("delivery_zip_code") !== false && getRequestParameter("delivery_zip_code") != "")
				{
					$params["delivery_zip_code"] = getRequestParameter("delivery_zip_code");
				}
				else
				{
					$errors["delivery_zip_code"] = translate("checkout_error_delivery_zip_code");
				}
				
				/*if(getRequestParameter("delivery_phone") !== false && getRequestParameter("delivery_phone") != "")
				{
					$params["delivery_phone"] = getRequestParameter("delivery_phone");
				}
				else
				{
					$errors["delivery_phone"] = translate("checkout_error_delivery_phone");
				}*/
				
			}
		}
		else
		{
			$errors["use_delivery_address"] = tranlsate("checkout_error_no_delivery_type");
		}
	}
?>